package com.marver.sys.service;

import com.marver.common.dao.BaseDAOImpl;
import com.marver.common.result.Result;
import com.marver.common.result.ResultStatus;
import com.marver.entity.Params;
import com.marver.entity.Wordbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ParamService extends BaseDAOImpl {
    
	public List<Params> getParams(Params param){
		return (List<Params>)this.listBySqlMap("params.getParams", param);
	}
	
	public String getParam(Params param){
		return (String)this.findObjectBySqlMap("params.getParam", param);
	}
	
	public String getParam(String code){
		Params param=new Params();
		param.setParamCode(code);
		return (String)this.findObjectBySqlMap("params.getParam", param);
	}
	
	public Map<String,String> getParamsByCode(String namespace,List param){
		List<Params> list=(List<Params>)this.listBySqlMap(namespace, param);
		Map<String,String> result=new HashMap<String,String>();
		for(Params s:list){
			result.put(s.getParamCode(), s.getParamValue());
		}
		return result;
	}	
	
	public Result addParam(Params param){
		Result resultMsg=new Result();
		if(param==null){
			resultMsg.setCode(ResultStatus.INVALID_PARAM.getErrorCode());
			resultMsg.getMessage().setTip("param参数为空！");
			return resultMsg;
		}
		if(this.findObjectBySqlMap("params.getByParaCode", param)!=null){
			resultMsg.setCode(ResultStatus.error_duplicated_data.getErrorCode());
			resultMsg.getMessage().setTip("参数已经存在！");
			return resultMsg;			
		}
		this.addBySqlMap("params.addParam", param);
		resultMsg=new Result(ResultStatus.SUCCESS.getErrorCode());  
		return resultMsg;
	}
	
	/**
	 * 根据类型查询数据字典
	 * @param param
	 * @return
	 */
	public Result getWordBook(String paratype){
		Wordbook wb=new Wordbook();
		wb.setParamType(paratype);
		return new Result(ResultStatus.SUCCESS.getErrorCode(),this.listBySqlMap("params.getWordBook", wb));  
	}
	
	
}
